<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Area extends Auth_Controller {
	
	function __construct(){
		parent::__construct();
		$this->load->model('area/AreaModel', '' , true);
	}

	/*
	 * 
	 */
	function index(){
		$result = $this->AreaModel->selectAll();
		//print_r($result);
		$view_data['list'] = $this->_build_tree($result);
		$view_data['action'] = 'list';
		$this->_view('manager/area/index',$view_data);
	}

	/*
	 * exist 检测快递名称是否存在
	 */
	function isExistName($name) {
		$name = urldecode($name);
		$data['state'] = $this->AreaModel->count(array('name'=>$name));
		$this->_output_json($data);
	}
	
	/**
	 * 新增或编辑显示父类
	 * @param $parentids 用“,”分隔的字符串.
	 */
	function _get_parent_names($parentids) {
		$parents = $this->AreaModel->selectByIds($parentids);
		$parents_name = '';
		$count = count($parents);
		foreach ($parents as $parent) {
			$count--;
			$parents_name .= $parent['name'].($count == 0 ? '':' » ');
		}
		return $parents_name;
	}
	
	/*
	 * 区域选择
	 */
	function choice($parentid=0) {
		$m = $this->input->get('m',true);//获取区域选择的方式，m=init 第一次请求
		if($m == 'init'){
			$parentname = '';
			$areas = '';
			if($parentid == 0 ){
				$areas = $this->AreaModel->selectByParam(array('parentid'=>$parentid));
			}else{
				$area = $this->AreaModel->selectById($parentid);
				if($area['parentid'] == 0){
					$parentname = $area['name'];
				}else{
					$parentname = $this->_get_parent_names($area['position']);
				}
				$areas = $this->AreaModel->selectByParam(array('parentid'=>0));
			}
			$data['parentname'] = $parentname;
			$data['areas'] = $areas;
			$this->_view('manager/area/area_choice',$data);
		}else{//ajax请求，改变分类
			$areas = $this->AreaModel->selectByParam(array('parentid'=>$parentid));
			$data['areas'] = $areas;
			$this->_output_json($data);
		}
	}
	
	function add($parentid=0) {
		$data = null;
		if($parentid != 0){
			$data['parentid'] = $parentid;
			$parent = $this->AreaModel->selectById($parentid);
			if(count($parent)){
				if($parent['position'] != $parentid){
					$data['parentname'] = $this->_get_parent_names($parent['position']);
				}else{
					$data['parentname'] = $parent['name'];
				}
			}
		}
		
		$view_data['form'] = $this->_form('area', '/manager/area/area/doadd', '/manager/area/bean/fb_area.php',$data);
		$view_data['action'] = 'add';
		$this->_view('manager/area/index',$view_data);
	}
	
	function edit($id=0) {
		$data = $this->AreaModel->selectById($id);
		if(count($data)){
			if($data['parentid']){
				$data['parentname'] = $this->_get_parent_names(str_replace(','.$id,'',$data['position']));
			}
			
			$view_data['form'] = $this->_form('area', '/manager/area/area/doedit', '/manager/area/bean/fb_area.php',$data);
			$view_data['action'] = 'edit';
			$this->_view('manager/area/index',$view_data);
		}else{
			$this->_view('error');
		}
	}
	
	/*
	 * add 新增
	 * 
	 * $redirect=''
	 */
	function doadd() {
		$area['parentid'] = $this->input->post('parentid',true);
		$area['name'] = $this->input->post('name',true);
		$area['code'] = $this->input->post('code',true);
		$area['sort'] = $this->input->post('sort',true);
		if($area['sort'] == '')
			$area['sort'] = 0;
		//insert 处理 position
		$data['state'] = $this->AreaModel->insert($area);
		
		$data['message'] = $data['state'] > 0? '新增区域成功。':'新增区域失败！';
		$this->_output_json($data);
	}
	
	function doedit() {
		$parentid = $_POST['parentid'];
		if($parentid){
			$parent = $this->AreaModel->selectById($parentid);
			$_POST['position'] = $parent['position'].','.$_POST['id'];
		}
		$data['state'] = $this->AreaModel->update($_POST);
		$data['message'] = $data['state'] > 0 ? '编辑区域成功。':'编辑区域失败！';
		$this->_output_json($data);
	}
	
	function dodelete($id) {
		$data['state'] = $this->AreaModel->delete($id);
		$data['message'] = $data['state'] > 0 ? '删除区域成功。':'删除区域失败！';
		$this->_output_json($data);
	}

}
?>